﻿using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Write.Api.Application.Command.ProjectCommand;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Write.Api.Application.CommandHander.ProjectCommandHander
{
    public class DelProjectCommandHander : IRequestHandler<DelProjectCommand, APIResult<int>>
    {
        private readonly ISqlSugarClient db;

        public DelProjectCommandHander(ISqlSugarClient db)
        {
            this.db = db;
        }

        public async Task<APIResult<int>> Handle(DelProjectCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var list = await db.Queryable<ProjectModel>().FirstAsync(x => x.ProjectId == request.ProjectId);

            list.Del = true;

            result.Data = await db.Updateable<ProjectModel>(list).ExecuteCommandAsync();

            if (result.Data > 0)
            {
                result.Code = APIEnum.success;
                result.Msg = "删除成功";
            }
            else
            {
                result.Code = APIEnum.error;
                result.Msg = "删除失败";
            }

            return result;
        }
    }
}
